package com.zhiyou100.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.zhiyou100.model.Project;
import com.zhiyou100.param.ProjectParam;
import com.zhiyou100.service.ProjectService;
import com.zhiyou100.vo.ResponseVo;

@RestController
public class ProjectController {
	
	@Autowired
	private ProjectService projectService;
	@GetMapping("/projects")
	public ResponseVo<ArrayList<Project>> findAllBy(ProjectParam pro){
		
		ArrayList<Project> al = projectService.findAllBy(pro);
		
		return new ResponseVo<ArrayList<Project>>(200, "請求成功", al);
		
	}
	@GetMapping("/projects/{id}")
	public ResponseVo<Project> findAll(@PathVariable int id){
		
		Project project = projectService.findAll(id);
		
		return new ResponseVo<Project>(200, "請求成功", project);
		
	}
	
	@PostMapping("/projects")
	public ResponseVo<String> add(Project project,MultipartFile file,HttpServletRequest request){
		
		String web=request.getServletContext().getRealPath("/");
		String path=web+"/"+file.getOriginalFilename();
		
		try {
			file.transferTo(new File(path));
		} catch (IllegalStateException | IOException e) {
			e.printStackTrace();
		}
		project.setLogoMobile(file.getOriginalFilename());
		project.setLogoWeb(file.getOriginalFilename());
		projectService.add(project);
		return new ResponseVo<String>(200, "修改成功",null);
		
	}
	
	@PostMapping("/projects")
	public ResponseVo<String> update(Project project,MultipartFile file,HttpServletRequest request){
		
		String web=request.getServletContext().getRealPath("/");
		String path=web+"/"+file.getOriginalFilename();
		
		try {
			file.transferTo(new File(path));
		} catch (IllegalStateException | IOException e) {
			e.printStackTrace();
		}
		project.setLogoMobile(file.getOriginalFilename());
		project.setLogoWeb(file.getOriginalFilename());
		projectService.update(project);
		return new ResponseVo<String>(200, "修改成功",null);
		
	}
	
	@PutMapping("/projects")
	public ResponseVo<String> updateStatus(@RequestBody int status){
		
		 projectService.updateStatus(status);
		
		return new ResponseVo<String>(200, "修改成功", null);
		
	}
	@DeleteMapping("/projects/{id}")
	public ResponseVo<String> delete(@PathVariable int id){
		
		projectService.delete(id);
		
		return new ResponseVo<String>(200, "删除成功", null);
		
		
		
	}
}
